package com.ss.android.article.base.utils.a;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ss.android.newmedia.BaseApplication;

public final class SharedPreferenceUtils {
    private static SharedPreferenceUtils instance;

    private SharedPreferenceUtils() {

    }

    public static SharedPreferenceUtils inst() {
        synchronized (SharedPreferenceUtils.class) {
            if (instance == null) {
                instance = new SharedPreferenceUtils();
            }
            return instance;
        }
    }

    public static String getString(String name, String key, String fallback) {
        return instance.getSharedPreference(name).getString(instance.getMigrateKey(name, key), fallback);
    }

    public static SharedPreferences getSharedPreference(String name) {
        if ((TextUtils.isEmpty(name)) || (b.isSpMigrated())) {
            name = "main_app_settings";
        }
        return BaseApplication.getApplication().getSharedPreferences(name, 0);
    }

    public static long getLong(String name, String key) {
        return instance.getSharedPreference(name).getLong(instance.getMigrateKey(name, key), 0);
    }

    public static SharedPreferences.Editor getEditor(String name) {
        return instance.getSharedPreference(name).edit();
    }

    public static String getMigrateKey(String name, String key) {
        if (!TextUtils.isEmpty(key) && !key.contains("@") && (b.isSpMigrated()) && !TextUtils.isEmpty((CharSequence) b.mPrefixMap.get(name))) {
            key = b.mPrefixMap.get(name) + key;
        }

        return key;
    }

    public static boolean getBoolean(String key) {
        return instance.getSharedPreference(null).getBoolean(instance.getMigrateKey(null, key), true);
    }

    public static int getInt(String key) {
        return instance.getSharedPreference(null).getInt(instance.getMigrateKey(null, key), 2);
    }
}

